﻿/*
 * =====================================================================================
 *
 *       Filename:  GetPathsTools.cs
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  
 *       Revision:  none
 *       Compiler:  
 *
 *         Author:   imbedinlove
 *        Company:  
 *
 * =====================================================================================

 */
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Configuration;
using System.DirectoryServices;
using ImbedLibrary;

namespace WebSiteTools
{
    public class GetTools
    {

        public static string EventPath()
        {
            string path = WebPath() + "\\Event\\";
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);
            return path;
        }

        /// <summary>
        /// Get
        /// 读取配置
        /// </summary>
        /// <param name="path">配置文件路径</param>
        /// <param name="key">key</param>
        /// <returns>value</returns>
        public static String Get(String path, String key)
        {
            using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read))
            {
                using (StreamReader sr = new StreamReader(fs, Encoding.UTF8))
                {
                    while (!sr.EndOfStream)
                    {
                        String sLine = sr.ReadLine().Trim();
                        if (sLine == "") continue;
                        if (sLine.IndexOf("#") == 0) continue;
                        if (sLine.Split('=')[0] == key) return sLine.Split('=')[1];
                    }
                    throw new Exception("没有找到" + key + "配置");
                }
            }
        }

        /// <summary>
        /// 网站数据库路径
        /// </summary>
        /// <param name="webName">站名</param>
        /// <returns>2|数据库路径 3|数据库路径</returns>
        public static String WebData(String webName)
        {
            try
            {
                Logs.Note("WebData:" + webName);
                String path = WebSitePath(webName).Trim();
                String[] a = { @"\Database\Global.asa", @"\Db\scjg_db.asa" };
                String tp = path + a[0].ToString();
                if (File.Exists(tp))
                    return "2|" + tp;
                tp = path + a[1].ToString();
                if (File.Exists(tp))
                    return "3|" + tp;
                throw new Exception("网站数据库不存在:" + path);
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 网站物理路径
        /// </summary>
        /// <param name="commentOfWebSite"></param>
        /// <returns></returns>
        public static string WebSitePath(string websiteName)
        {
            Logs.Note("WebSitePath:" + websiteName);
            String metaBasePath = String.Format("IIS://Localhost/W3SVC/{0}/root", WebSiteID(websiteName));
            using (DirectoryEntry de = new DirectoryEntry(metaBasePath))
            {
                return de.Properties["Path"].Value.ToString();
            }
            throw new Exception("没有找到网站: " + websiteName);
        }

        /// <summary>
        /// WebSiteID
        /// </summary>
        /// <param name="websiteName">站名</param>
        /// <returns></returns>
        public static String WebSiteID(String websiteName)
        {
            DirectoryEntry w3svc = new DirectoryEntry("IIS://localhost/w3svc");

            foreach (DirectoryEntry de in w3svc.Children)
            {
                if (de.SchemaClassName == "IIsWebServer" && de.Properties["ServerComment"][0].ToString() == websiteName)
                {
                    return de.Name;
                }

            }
            throw new Exception("没有找到网站");
        }

        /// <summary>
        /// 网站路径
        /// </summary>
        /// <param name="siteNameJianPin">网站简拼</param>
        /// <returns></returns>
        public static string WebsitePath(string siteNameJianPin)
        {
            try
            {
                return AppConfig("websitePath") + siteNameJianPin;
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 获取模板文件路径
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static string templateFile(string name)
        {
            return AppConfig("templatePath") + name + ".zip";
        }

        /// <summary>
        /// 获取网站xml文件物理路径
        /// </summary>
        /// <returns>网站物理路径</returns>
        public static string WebXmlPath()
        {
            return WebPath() + AppConfig("errerPath") + "\\";
        }

        /// <summary>
        /// 获取应用程序执行路径
        /// </summary>
        /// <returns></returns>
        public static string WebPath()
        {
            return System.AppDomain.CurrentDomain.BaseDirectory;
        }

        /// <summary>
        /// 获得配置信息
        /// </summary>
        /// <param name="name">节点名称</param>
        /// <returns></returns>
        public static string AppConfig(string name)
        {
            return ConfigurationManager.ConnectionStrings[name].ToString();
        }

        /// <summary>
        /// 记录文件夹
        /// </summary>
        /// <returns></returns>
        public static string NoteChange()
        {
            string path = Directory.GetCurrentDirectory() + "\\修改域名\\";
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);
            return path;
        }
    }
}
